import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Producer {
    public static void main(String[] args) throws IOException, TimeoutException {
        //1.建立链接
        ConnectionFactory connectionFactory = new ConnectionFactory();
        //主机地址
        connectionFactory.setHost("139.9.116.124");
        //端口号
        connectionFactory.setPort(5672);
        //用户名
        connectionFactory.setUsername("test");
        //密码
        connectionFactory.setPassword("123456");
        //虚拟机
        connectionFactory.setVirtualHost("test");
        //连接建立
        Connection connection = connectionFactory.newConnection();
        //2.开启信道
        Channel channel = connection.createChannel();
        //3.声明交换机,使用内置交换机
        //4.声明队列
        channel.queueDeclare("test1",true,false,false,null);
        //5.发送消息
        for (int i = 0; i < 10; i++) {
            String msg = "hello RabbitMQ~";
            channel.basicPublish("","test1",null,msg.getBytes());
        }
        System.out.println("消息发送成功!");
        //6.资源释放
        channel.close();
        connection.close();
    }
}
